layui.use(['table', 'admin', 'ztreeDistrict','ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztreeDistrict;
    var func = layui.func;

    /**
     * 管理
     */
    var SysDistrict = {
        tableId: "sysDistrictTable",
        condition: {
            id: ""
        }
    };

    /**
     * 初始化表格的列
     */
    SysDistrict.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'districtName', sort: true, title: '区划名称 '},
            {field: 'districtCode', sort: true, title: '区划编号'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };




    /**
     * 点击查询按钮
     */
    SysDistrict.search = function () {
        var queryData = {};
        queryData['condition'] = $("#name").val();
        queryData['code'] = $("#code").val();
        queryData['id'] = "";
        table.reload(SysDistrict.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击城市按钮
     */
    SysDistrict.searchById = function () {
        var queryData = {};
        queryData['condition'] = "";
        queryData['code'] = "";
        queryData['id'] = SysDistrict.condition.id;
        table.reload(SysDistrict.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 选择城市时
     */
    SysDistrict.onClickSysDistrict = function (e, treeId, treeNode) {
        SysDistrict.condition.id = treeNode.id;
        SysDistrict.searchById();
    };


    /**
     * 弹出添加对话框
     */
    SysDistrict.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/sysDistrict/add?pid='+SysDistrict.condition.id,
            tableId: SysDistrict.tableId,
            endCallback: function () {
                var obj = $.fn.zTree.getZTreeObj("sysDistrictTree");
                //获取参数
                var object = admin.getTempData('district');
                var jsonData = JSON.stringify(object);
                var result = $.parseJSON(jsonData);

                var node = obj.getNodeByParam("id", result.data.parentId, null);
                var name=result.data.districtName;
                var newNode = {name:name,id:result.data.districtId};

                obj.addNodes(node,newNode,false);
            }
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    SysDistrict.openEditDlg = function (data) {
        func.open({
            title: '修改',
            content: Feng.ctxPath + '/sysDistrict/edit?districtId=' + data.districtId,
            tableId: SysDistrict.tableId,
            endCallback: function () {
                var obj = $.fn.zTree.getZTreeObj("sysDistrictTree");
                var node = obj.getNodeByParam("id", data.districtId, null);
                var object = admin.getTempData('name');
                var jsonData = JSON.stringify(object);
                var result = $.parseJSON(jsonData);
                node.name=result.data;
                obj.updateNode(node);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    SysDistrict.exportExcel = function () {
        var checkRows = table.checkStatus(SysDistrict.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    SysDistrict.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysDistrict/delete", function (data) {

                var jsonData = JSON.stringify(data);
                var result = $.parseJSON(jsonData);
                var obj = $.fn.zTree.getZTreeObj("sysDistrictTree");
                var node = obj.getNodeByParam("id", result.data, null);
                obj.removeNode(node);


                Feng.success("删除成功!");
                table.reload(SysDistrict.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("districtId", data.districtId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    //初始化左侧部门树
    var ztree = new $ZTree("sysDistrictTree", "/sysDistrict/tree");
    ztree.bindOnClick(SysDistrict.onClickSysDistrict);
    ztree.init();

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SysDistrict.tableId,
        url: Feng.ctxPath + '/sysDistrict/list?parentId=' + SysDistrict.condition.id,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: SysDistrict.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SysDistrict.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        SysDistrict.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        SysDistrict.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + SysDistrict.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            SysDistrict.openEditDlg(data);
        } else if (layEvent === 'delete') {
            SysDistrict.onDeleteItem(data);
        }
    });

});
